package com.ruoyi.buss.grid.zysc.service.impl;

import java.text.SimpleDateFormat;
import java.util.*;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ruoyi.buss.grid.cj.domain.DataWgWgcj;
import com.ruoyi.buss.grid.zysc.domain.DataWgZysc;
import com.ruoyi.buss.grid.zysc.domain.DataWgZyscKh;
import com.ruoyi.buss.grid.zysc.domain.DataWgZyscSurvey;
import com.ruoyi.buss.grid.zysc.mapper.DataWgZyscKhMapper;
import com.ruoyi.buss.grid.zysc.mapper.DataWgZyscMapper;
import com.ruoyi.buss.grid.zysc.service.IDataWgZyscService;
import com.ruoyi.buss.utils.DateUtilss;
import com.ruoyi.buss.utils.GetOnlyId;
import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.system.api.model.LoginUser;
import org.apache.commons.collections4.Get;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 网格专业市场Service业务层处理
 * 
 * @author ruoyi
 * @date 2021-09-26
 */
@Service
public class DataWgZyscServiceImpl implements IDataWgZyscService
{
    @Autowired
    private DataWgZyscMapper dataWgZyscMapper;

    @Autowired
    private DataWgZyscKhMapper dataWgZyscKhMapper;

    /**
     * 查询网格专业市场
     * 
     * @param marketId 网格专业市场ID
     * @return 网格专业市场
     */
    @Override
    public DataWgZysc selectDataWgZyscById(String marketId)
    {
        return dataWgZyscMapper.selectDataWgZyscById(marketId);
    }

    /**
     * 查询网格专业市场列表
     * 
     * @param dataWgZysc 网格专业市场
     * @return 网格专业市场
     */
    @Override
    public List<DataWgZysc> selectDataWgZyscList(DataWgZysc dataWgZysc, LoginUser userInfo)
    {
        String orgId = userInfo.getSysUser().getDept().getOrgId();
        String userId = userInfo.getSysUser().getUserName();
        // 如果是admin用户不做处理
        if (userInfo.getRoles().contains("zhhz")) {
            dataWgZysc.setOrgId(orgId);
        } else if (userInfo.getRoles().contains("common")) {
            dataWgZysc.setOrgId(orgId);
            dataWgZysc.setManagerId(userId);
        }
        return dataWgZyscMapper.selectDataWgZyscList(dataWgZysc);
    }
    @Override
    public List<DataWgZyscKh> queryGridZyscKhAll(DataWgZyscKh dataWgZyscKh, LoginUser userInfo){

        String orgId = userInfo.getSysUser().getDept().getOrgId();
        String userId = userInfo.getSysUser().getUserName();
        // 如果是admin用户不做处理
        if (userInfo.getRoles().contains("zhhz")) {
            dataWgZyscKh.setOrgId(orgId);
        } else if (userInfo.getRoles().contains("common")) {
            dataWgZyscKh.setOrgId(orgId);
            dataWgZyscKh.setManagerId(userId);
        }
        List<DataWgZyscKh> dataWgZyscKhList = dataWgZyscKhMapper.queryGridZyscKhAll(dataWgZyscKh);

        return dataWgZyscKhList;
    }

    public Map<String, Object> queryGridZyscList(DataWgZyscKh dataWgZyscKh, LoginUser userInfo) {
        String orgId = userInfo.getSysUser().getDept().getOrgId();
        String userId = userInfo.getSysUser().getUserName();

        List<DataWgZyscKh> doList = new ArrayList<DataWgZyscKh>();
        // 如果是admin用户不做处理
        if (userInfo.getRoles().contains("zhhz")) {
            dataWgZyscKh.setOrgId(orgId);
        } else if (userInfo.getRoles().contains("common")) {
            dataWgZyscKh.setOrgId(orgId);
            dataWgZyscKh.setManagerId(userId);
        }
        Map<String, Object> map = new HashMap<String, Object>();
        /**
         * 专业市场 每个角色都可以查看多个专业市场 marketId如果为空 表示查询但是专业市场，返回该专业市场概况与专业市场客户 marketId不为空
         * 表示查询当前角色所拥有的所有专业市场 返回所有专业市场概况与专业市场概况列表
         */
        DataWgZyscSurvey dataWgZyscSurvey = new DataWgZyscSurvey();
        if (dataWgZyscKh != null && StringUtils.isNotEmpty(dataWgZyscKh.getMarketId())) {
            doList = dataWgZyscKhMapper.queryGridZyscKhAll(dataWgZyscKh);
            dataWgZyscSurvey = dataWgZyscKhMapper.getSurvey(dataWgZyscKh);
            map.put("dataWgZyscSurvey", dataWgZyscSurvey); // 支行概况
            map.put("pageCommonVO", doList);// 支行商户列表
        } else {
            // 支行概况列表
            List<DataWgZyscSurvey> dataWgZyscSurveyList = dataWgZyscKhMapper.getSurveyList(dataWgZyscKh);
            // 概况
            dataWgZyscSurvey = dataWgZyscKhMapper.getSurvey(dataWgZyscKh);

            map.put("dataWgZyscSurvey", dataWgZyscSurvey); // 概况
            map.put("pageCommonVO", dataWgZyscSurveyList);// 支行概况列表
        }


        return map;
    }


    public List<DataWgZyscSurvey> zyscAppBiYear(LoginUser userInfo) {

        String orgId = null;
        String userId = null;

        if (userInfo.getRoles().contains("zhhz")) {
            orgId = userInfo.getSysUser().getDept().getOrgId();

        } else if (userInfo.getRoles().contains("common")) {
            orgId = userInfo.getSysUser().getDept().getOrgId();
            userId = userInfo.getSysUser().getUserName();
        }
        Date date = new Date();
        SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd");
        String twoYearAgo = DateUtilss.getLastYear(new Date(), -2);// 俩年前
        String ontYearAgo = DateUtilss.getLastYear(new Date(), -1);// 一年前
        String today = formatter.format(date).toString();
        List<DataWgZyscSurvey> list = dataWgZyscKhMapper.zyscAppBiYear(twoYearAgo, ontYearAgo, today, orgId, userId);

        return list;
    }


    public List<DataWgZyscSurvey> zyscAppBiJd(LoginUser userInfo) {
        String orgId = null;
        String userId = null;

        if (userInfo.getRoles().contains("zhhz")) {
            orgId = userInfo.getSysUser().getDept().getOrgId();

        } else if (userInfo.getRoles().contains("common")) {
            orgId = userInfo.getSysUser().getDept().getOrgId();
            userId = userInfo.getSysUser().getUserName();
        }
        Date date = new Date();
        SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd");
        String fjd = DateUtilss.getLastDay(new Date(), -9);// 4季度
        String sjd = DateUtilss.getLastDay(new Date(), -6);// 3季度
        String tjd = DateUtilss.getLastDay(new Date(), -4);// 2季度 二季度减四
        String today = formatter.format(date).toString();
        List<DataWgZyscSurvey> list = dataWgZyscKhMapper.zyscAppBiJd(fjd, sjd, tjd, today, orgId, userId);
        return list;
    }


    public List<DataWgZyscSurvey> zyscAppBiMonth(LoginUser userInfo) {
        String orgId = null;
        String userId = null;

        if (userInfo.getRoles().contains("zhhz")) {
            orgId = userInfo.getSysUser().getDept().getOrgId();

        } else if (userInfo.getRoles().contains("common")) {
            orgId = userInfo.getSysUser().getDept().getOrgId();
            userId = userInfo.getSysUser().getUserName();
        }
        /**
         * 获取今年内每个月最后一天
         */
        ArrayList<String> sjrqs = DateUtilss.getEndMonthByYear(1, 0);

        List<DataWgZyscSurvey> list = dataWgZyscKhMapper.zyscAppBiMonth(sjrqs, orgId, userId);
        return list;
    }

    /**
     * 新增网格专业市场
     * 
     * @param dataWgZysc 网格专业市场
     * @return 结果
     */
    @Override
    public int insertDataWgZysc(DataWgZysc dataWgZysc)
    {
        dataWgZysc.setCreateTime(DateUtils.getNowDate());
        dataWgZysc.setMarketId(GetOnlyId.getOnlyId());
        System.out.println(dataWgZysc.toString());
        return dataWgZyscMapper.insertDataWgZysc(dataWgZysc);
    }

    /**
     * 修改网格专业市场
     * 
     * @param dataWgZysc 网格专业市场
     * @return 结果
     */
    @Override
    public int updateDataWgZysc(DataWgZysc dataWgZysc)
    {
        dataWgZysc.setUpdateTime(DateUtils.getNowDate());
        return dataWgZyscMapper.updateDataWgZysc(dataWgZysc);
    }

    /**
     * 批量删除网格专业市场
     * 
     * @param marketIds 需要删除的网格专业市场ID
     * @return 结果
     */
    @Override
    public int deleteDataWgZyscByIds(String[] marketIds)
    {
        return dataWgZyscMapper.deleteDataWgZyscByIds(marketIds);
    }

    /**
     * 删除网格专业市场信息
     * 
     * @param marketId 网格专业市场ID
     * @return 结果
     */
    @Override
    public int deleteDataWgZyscById(String marketId)
    {
        return dataWgZyscMapper.deleteDataWgZyscById(marketId);
    }
}
